//
// Created by Administrator on 2022-11-29.
//
#include "CCRender.h"

CCRender::CCRender() {
    ccVBO = new CCOpenGLBuffer(CCOpenGLBuffer::VertexBuffer,CCOpenGLBuffer::StaticDraw);
    ccEBO = new CCOpenGLBuffer(CCOpenGLBuffer::IndexBuffer,CCOpenGLBuffer::StaticDraw);
}

CCRender::~CCRender(){
    release();
}

/**
 * 加载
 */
void CCRender::initGL()
{
    //清空颜色
    glClearColor(0.0,0,0,1.0);
    //清空深度缓存
    glClearDepthf(1.0);
    //开启深度测试
    glEnable(GL_DEPTH_TEST);
    //什么情况下使用深度测试
    glDepthFunc(GL_LEQUAL);

    setupRenderingObject();
}

/**
 * 先加载VAO VBO EBO等缓存数据
 */
void CCRender::setupRenderingObject()
{
    //顶点数据
    const CCFloat5 cubeVertexs[]  = {
            {  -540,-1045, 1.0 ,  0.0, 0.0 },
            {  -540, 1045, 1.0 ,  0.0, 1.0 },
            {  540, -1045,  1.0 , 1.0, 0.0 },
            {   540, 1045, 1.0 ,  1.0, 1.0 },
    };

    //索引数据
    const short cubeIndexs[]= {
            0, 1, 2,  2, 1, 3,
    };

    ccVAO.Create();
    ccVAO.Bind();

    ccVBO->Create();
    ccVBO->Bind();
    ccVBO->SetBufferData(cubeVertexs,sizeof(cubeVertexs));


    ccEBO->Create();
    ccEBO->Bind();
    ccEBO->SetBufferData(cubeIndexs,sizeof(cubeIndexs));

    //数据的位置
    int offset = 0;

    ccOpenGlShader.SetAttributeBuffer(0,GL_FLOAT, (void *)offset, 3, sizeof(CCFloat5));
    ccOpenGlShader.EnableAttributeArray(0);

    //由于数据类型为float，而uv数据的在第四第五的位置，因此需要偏移3个
    offset += 3 * sizeof(float);

    ccOpenGlShader.SetAttributeBuffer(1,GL_FLOAT, (void *)offset, 2, sizeof(CCFloat5));
    ccOpenGlShader.EnableAttributeArray(1);

    ccVAO.Release();
    ccVBO->Release();
    ccEBO->Release();
}

/**
 * 绘制
 */
void CCRender::paintGL()
{
    //清空颜色缓冲区或深度缓冲区
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    drawShaderAnim();
}

/**
 * Shader动画
 */
void CCRender::drawShaderAnim() {
//    m_angle += 0.05f;

    u_value += 0.05f;
//    if(u_value >= 1.0){
//        u_value = 1.0;
//    }

    glm::mat4x4  objectMat;
    glm::mat4x4  objectTransMat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -1));
    glm::mat4x4  objectRotMat = glm::rotate(glm::mat4(1.0f),m_angle,glm::vec3(1.0f, 1.0f, 1.0) );
    glm::mat4x4  objectScaleMat = glm::scale(glm::mat4(1.0f),glm::vec3(1.0f, 1.0f, 0.0) );

//    glm::mat4 projMat = glm::perspective(glm::radians(60.0f), (float)9/(float)18, 0.0f, 1000.0f);

    float halfWidth = screenWidth / 2.0f;
    float halfHeight = screenHeight / 2.0f;

    //以当前屏幕宽高设置坐标
    glm::mat4 projMat = glm::ortho(-halfWidth, halfWidth, -halfHeight, halfHeight, 0.1f, 100.0f);

    objectMat = projMat* objectTransMat * objectScaleMat* objectRotMat ;

    ccOpenGlShader.Bind();

    ccOpenGlShader.SetUniformValue("u_mat",objectMat);
    //设置变化的值
    ccOpenGlShader.SetUniformValue("uValue",u_value);

    //设置iResolution
    ccOpenGlShader.SetUniformValue("iResolution",iResolution);

    ccVAO.Bind();

    glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_SHORT,(void *)0);
    ccOpenGlShader.Release();
    ccVAO.Release();
}


/**
 * 窗口大小改变
 */
void CCRender::resizeGL(int width,int height)
{
    screenWidth = width;
    screenHeight = height;
    //设置窗口大小
    glViewport(0,0,width,height);
}

/**
 * 释放资源
 */
void CCRender::release()
{
    glDeleteTextures(6,ccTexture.m_texID);
    delete ccVBO;
    delete ccEBO;
}
